package com.sgb.kjwl.aroute;

import static com.swgk.core.util.ThreadUtils.runOnUiThread;
import android.app.Activity;
import android.content.Context;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.aroute.provider.ShareProvider;
import com.swgk.core.util.MToast;
import java.util.HashMap;
import cn.sharesdk.dingding.friends.Dingding;
import cn.sharesdk.dingding.utils.DingdingClientNotExistException;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

/**
 * Time:2022/2/10
 * AUTHOR:F_Q
 * Description:
 */
@Route(path =  AppRouteConstant.SHAREDIALOG, name = "分享跳转")
public class ShareProviderImpl implements ShareProvider {
    private Platform platform;

    //分享实现
    @Override
    public void startShareData(Activity context, String title, String url, String cover, int i) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(title);
        sp.setText(url);
        sp.setImageUrl(cover);
        sp.setUrl(url);
        sp.setTitleUrl(url);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        switch (i) {
            case 1://微信
                platform = ShareSDK.getPlatform(Wechat.NAME);
                break;
            case 2:////钉钉
                platform = ShareSDK.getPlatform(Dingding.NAME);
                break;
        }

        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Tools(context,"分享成功！");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                if (throwable instanceof WechatClientNotExistException) {
                    Tools(context,"微信未安装！");
                }
                if (throwable instanceof DingdingClientNotExistException) {
                    Tools(context,"钉钉未安装！");
                }
                if ("QQClientNotExistException".equals(throwable.getMessage())) {
                    Tools(context,"QQ未安装！");
                }
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Tools(context,"分享取消！");
            }
        });
        platform.share(sp);
    }

    private void Tools(Activity context, String name) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MToast.showToast(context,name);
            }
        });
    }

    @Override
    public void init(Context context) {

    }
}
